package com.etlmaster.executor.utils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;

import org.apache.log4j.Appender;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.DailyRollingFileAppender;


/**
 * 日志输出类
 * 
 * @author Wugf
 * @date 2016-03-08
 */
public class LogWriter {
	private static Map<String,Logger> LOG = new HashMap<String,Logger>();

	public static String defaultLogger="etlmaster";
	static class LogEntity {
		public String level;
		public String message;

		public LogEntity(String level, String message) {
			this.level = level;
			this.message = message;
		}
	}
	static{
		initLogger(defaultLogger,false);
	}
	public static void initLogger(String jobId,boolean isDebug) {
		 Logger logger = Logger.getLogger(String.valueOf(jobId));  
	     logger.removeAllAppenders();  
	     logger.setAdditivity(true);//设置继承输出root  
	     Appender fileAppender = null;  
	     Appender consoleAppender=null;
	     PatternLayout layout = new PatternLayout();  
	     layout.setConversionPattern("%d{yyyy-MM-dd HH:mm:ss} [%t] [%p] %m%n");  
	     try {
	    	 fileAppender =new DailyRollingFileAppender(layout,"logs/"+jobId+".log","yyyy-MM-dd");
	    	 consoleAppender=new ConsoleAppender(layout);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	    logger.addAppender(consoleAppender);
	    logger.addAppender(fileAppender);  
	    if(isDebug)
	    	logger.setLevel(Level.DEBUG);  
	    else
	    	logger.setLevel(Level.INFO);
		LOG.put(jobId, logger);
		defaultLogger=jobId;
	    addLog("INFO","{} logger创建成功！",jobId);
	}
	private LogWriter(){
		
	}
	public static void addLog(String...logs ){
		addLog(defaultLogger,0,logs);
	}

	public static void addLog(String jobID,int counter,String... logs) {
		if (logs.length >= 2) {
			String level = logs[0];
			String message = logs[1];

			for (int i = 0; i < logs.length - 2; i++) {
				String log = logs[i + 2];
				if(!(log==null))
					message = message.replaceFirst("\\{\\}", Matcher.quoteReplacement(log));
				else
					message = message.replaceFirst("\\{\\}","null");
			}
			LogEntity entity = new LogEntity(level, message);
			write(jobID,entity);
		}else{
			LOG.get(jobID).warn("日志语句未到最小参数");
		}
	}
	public static void updateLogLevel(String jobId,String level){
		if(LOG.get(jobId)!=null){
			LOG.get(jobId).setLevel(Level.toLevel(level));
		}
	}
	private synchronized static void write(String jobId,LogEntity entity){
		String level = entity.level;
		if(LOG.get(jobId)==null)
			jobId=defaultLogger;
		if (level.equalsIgnoreCase("ERROR")) {
			LOG.get(jobId).error(entity.message);
		} else if (level.equalsIgnoreCase("DEBUG")) {
			LOG.get(jobId).debug(entity.message);
		} else if (level.equalsIgnoreCase("WARN")) {
			LOG.get(jobId).warn(entity.message);
		} else if (level.equalsIgnoreCase("INFO")){
			LOG.get(jobId).info(entity.message);
		}else {
			LOG.get(jobId).warn("不支持的日志等级:"+level);
		}
	}
	public  static void main(String []args){
		LogWriter.addLog("INFO","测试");
	}
}
